package cn.ytr.project.web.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.ytr.project.domain.Product;
import cn.ytr.project.mapper.ProductMapper;
import cn.ytr.project.query.PageResult;
import cn.ytr.project.query.ProductQueryObject;
import cn.ytr.project.service.IProductService;
import cn.ytr.project.service.impl.ProductServiceImpl;
import cn.ytr.project.util.MybatisUtil;
import cn.ytr.project.util.StringUtil;

@WebServlet("/product")
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IProductService service = new ProductServiceImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String cmd = req.getParameter("cmd");
		if ("delete".equals(cmd)) {
			delete(req, resp);
		} else if ("input".equals(cmd)) {
			input(req, resp);
		} else if ("saveOrUpdate".equals(cmd)) {
			saveOrUpdate(req, resp);
		} else {
			list(req, resp);
		}

	}

	public void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.获取请求参数封装成对象
		Product p = new Product();
		// 字符串类型的不用校验
		String productName = req.getParameter("productName");
		String supplier = req.getParameter("supplier");
		String brand = req.getParameter("brand");
		p.setProductName(productName);
		p.setSupplier(supplier);
		p.setBrand(brand);

		// 做简单的校验
		String salePrice = req.getParameter("salePrice");
		String costPrice = req.getParameter("costPrice");
		String cutoff = req.getParameter("cutoff");
		String dirId = req.getParameter("dirId");
		if (StringUtil.hasLength(salePrice)) {
			p.setSalePrice(new BigDecimal(salePrice));
		}
		if (StringUtil.hasLength(costPrice)) {
			p.setCostPrice(new BigDecimal(costPrice));
		}
		if (StringUtil.hasLength(cutoff)) {
			p.setCutoff(new BigDecimal(cutoff));
		}
		if (StringUtil.hasLength(dirId)) {
			p.setDirId(Long.valueOf(dirId));
		}

		String sid = req.getParameter("id");
		// 2.调用业务方法处理请求
		// 如果是保存,调用save方法,如果是更新,调用update方法
		if (StringUtil.hasLength(sid)) {
			p.setId(Long.valueOf(sid));
			service.update(p);
		} else {
			service.save(p);
		}
		// 3.控制界面跳转
		resp.sendRedirect("/product");
	}

	// 点击添加按钮和编辑按钮都执行该方法
	public void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String sid = req.getParameter("id");
		if (StringUtil.hasLength(sid)) {
			// 点击编辑按钮
			// 如果是点击编辑按钮,获取到id值,把id对应的商品的信息获取出来,并且交给输入界面显示出来
			Long id = Long.valueOf(sid);
			Product p = service.get(id);
			req.setAttribute("p", p);
			req.getRequestDispatcher("/WEB-INF/views/productInput.jsp").forward(req, resp);
		} else {
			// 点击添加按钮
			// 如果是点击添加按钮,进入到输入界面
			req.getRequestDispatcher("/WEB-INF/views/productInput.jsp").forward(req, resp);
		}
	}

	public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 把要删除的商品的id获取出来
		String sid = req.getParameter("id");
		if (StringUtil.hasLength(sid)) {
			Long id = Long.valueOf(sid);
			service.delete(id);
		}
		// 控制界面跳转.重定向到/product
		resp.sendRedirect("/product");
		// req.getRequestDispatcher("/productabc").forward(req, resp);

	}
	
	//测试PageHelper 插件
	@Test
	public void testPageHelper() throws Exception {
		SqlSession session = MybatisUtil.getSession();
		ProductMapper productMapper = session.getMapper(ProductMapper.class);
		PageHelper.startPage(1, 10);
		List<Product> all = productMapper.listAll();
		PageInfo page = new PageInfo(all);
		for (Product product : all) {
			System.out.println(product);
		}
		
		System.out.println(page);
	}

	public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//接收并封装高级查询的查询
		String keyword = req.getParameter("keyword");
		String minSalePrice = req.getParameter("minSalePrice");
		String maxSalePrice = req.getParameter("maxSalePrice");
		
		ProductQueryObject qo = new ProductQueryObject();
		qo.setKeyword(keyword);
		if(StringUtil.hasLength(minSalePrice)){
			qo.setMinSalePrice(new BigDecimal(minSalePrice));
		}
		
		if(StringUtil.hasLength(maxSalePrice)){
			qo.setMaxSalePrice(new BigDecimal(maxSalePrice));
		}
		
		
		// 1. 获取请求参数封装成对象
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
		//QueryObject qo = new QueryObject();
		if (StringUtil.hasLength(currentPage)) {
			qo.setCurrentPage(Integer.valueOf(currentPage));
		}
		if(StringUtil.hasLength(pageSize)){
			qo.setPageSize(Integer.valueOf(pageSize));
		}
		// 2. 调用业务方法处理请求
		PageResult result = service.query(qo);
		req.setAttribute("result", result);
		
		//将高级查询对象共享给页面,页面做数据回显
		req.setAttribute("qo", qo);
		// 3. 控制界面跳转
		req.getRequestDispatcher("/WEB-INF/views/productList.jsp").forward(req, resp);
	}

	public void list_bak(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1. 获取请求参数封装成对象
		// 2. 调用业务方法处理请求
		List<Product> list = service.listAll();
		req.setAttribute("list", list);
		// 3. 控制界面跳转
		req.getRequestDispatcher("/WEB-INF/views/productList.jsp").forward(req, resp);
	}

}
